//-----------------------------------------------------------------------

// <copyright file="ProgressBarFieldDefinition.cs" company="Jamie Knight">

// Copyright (c) Jamie Knight. All rights reserved.

// </copyright>

//-----------------------------------------------------------------------

using System;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;

namespace DevKnightlie.WPF.AutoForm.FieldDefinitions
{
    /// <summary>
    /// Field Definition class which builds a progress bar
    /// </summary>
    public class ProgressBarFieldDefinition: BaseFieldDefinition
    {
        #region Private Fields

        private int _minimum;
        private int _maximum;
        private bool _showLabel;
        private string _indeterminatePropertyname;

        #endregion

        #region Properties

        /// <summary>
        /// Progress Bar minimum value
        /// </summary>
        public int Minimum
        {
            get { return _minimum; }
            set { _minimum = value; }
        }

        /// <summary>
        /// Progress Bar maximum value
        /// </summary>
        public int Maximum
        {
            get { return _maximum; }
            set { _maximum = value; }
        }

        /// <summary>
        /// Sets or returns a value indicating whether a label should appear next to the progress bar
        /// </summary>
        public bool ShowLabel
        {
            get { return _showLabel; }
            set { _showLabel = value; }
        }

        /// <summary>
        /// Sets or returns the name of a property to bind the progress bars Indeterminate property to. Lets the data model control whether the progress bar is indeterminate or has a value
        /// </summary>
        public String IndeterminatePropertyName
        {
            get { return _indeterminatePropertyname; }
            set { _indeterminatePropertyname = value; }
        }

        #endregion

        public override void BuildInputControl()
        {
            ProgressBar bar = new ProgressBar()
            {
                Minimum = Minimum,
                Maximum = Maximum,
                Value = 0,
                Height=20
            };
            Binding barBinding = new Binding(PropertyName);
            BindingOperations.SetBinding(bar, RangeBase.ValueProperty, barBinding);
            SetupEnabledBinding(bar);
            if (!String.IsNullOrWhiteSpace(IndeterminatePropertyName))
            {
                Binding indeterminateBinding = new Binding(IndeterminatePropertyName);
                BindingOperations.SetBinding(bar, ProgressBar.IsIndeterminateProperty, indeterminateBinding);
            }
            ContainerControl = InputControl = bar;

        }

        public override void BuildLabelControl()
        {
            if (ShowLabel)
            {
                base.BuildLabelControl();
            }
            else
            {
                LabelControl = null;
            }
        }

    }
}
